package com.ktjy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.pages.ProjectPage;
import com.ktjy.pojo.Product;
import com.ktjy.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 查询产品信息
 */
@Controller
public class ProductController {
    @Autowired
    ProductService productService;

    @RequestMapping("/product/list")
    public Object productList(Product product,@RequestParam(defaultValue = "1") Integer pageIndex,@RequestParam(defaultValue = "3") Integer pageSize, Model model) {
        IPage<Product> productIPage = new Page<>(pageIndex,pageSize);
        ProjectPage<Product> projectPage = new ProjectPage<Product>();
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("prod_name", product.getProdName());
        queryWrapper.like("prod_type", product.getProdType());
        queryWrapper.like("prod_batch", product.getProdBatch());
        Page<Product> page = (Page<Product>) productService.page(productIPage,queryWrapper);
        BeanUtils.copyProperties(page,projectPage );
        model.addAttribute("productPage",projectPage);
        model.addAttribute("prodName",product.getProdName());
        model.addAttribute("prodType",product.getProdType());
        model.addAttribute("prodBatch",product.getProdBatch());
        return "/product/list";
    }
}
